<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 *
 *
 *
 * @package     h
 * @subpackage  models
 * @category    models
 * @author      hyw
 * @copyright   copyright (c) 2012
 * @filesource
 */
class Role_model extends Base_model {

	protected $_table_name = 'role';
	protected $_key_name = 'role_id';

	function get_info($id) {
		if (!empty($id))
			$where[$this -> _key_name] = $id;
		$where["role_dtime"] = 0;
		return $this -> fetch(NULL, $where, NULL, 'row');
	}

	function get_all() {
		$this -> db -> join('department', 'role.department_id = department.department_id');
		$this -> set_order_by('role.sequence,ASC');
		$where = array("role_dtime" => 0);
		return $this -> fetch(NULL, $where);
	}

	function query_keyword($keyword = "", $limit) {
		$this -> set_or_like('role_name,description', $keyword);
		$this -> set_limit($limit);
		$where["role_dtime"] = 0;
		return $this -> fetch(NULL, $where);
	}

	function query_keyword_count($keyword = null) {
		$this -> set_or_like('role_name,description', $keyword);
		$con["where"][] = array("role_dtime" => 0);
		$this -> set_args_array($con);
		return $this -> fetch_count();
	}

	function update_data($data) {

		$value = array('role_name' => $data['role_name'], 'department_id' => $data['department_id'], 'description' => $data['description'], 'role_mtime' => time());
		$where[] = array($this -> _key_name => $data['id']);
		return $this -> update($value, $where);
	}

	function update_purview($data) {
		$value = array('purview' => $data['purview'], 'role_mtime' => time());
		$where[] = array($this -> _key_name => $data['id']);
		return $this -> update($value, $where);
	}

	

	function add($data) {
		$data["role_ctime"] = time();
		return $this -> insert($data);
	}

	function del($data) {

		$value = array("role_dtime" => time());
		$this -> db -> where($this -> _key_name, $data['id']);
		$this -> db -> update($this -> _table_name, $value);
		return 1;

	}

	function update($data, $id) {

		$this -> db -> where($this -> _key_name, $id);
		$this -> db -> update($this -> _table_name, $data);

	}

}
